package com.zpark.utils;

import com.zpark.entity.patients.Patients;
import com.zpark.service.PatientsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Component
public class SecurityUtil {

    private static PatientsService patientsService;

    @Autowired
    public void setPatientsService(PatientsService patientsService) {
        SecurityUtil.patientsService = patientsService;
    }

    public static Long getCurrentPatientId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.getPrincipal() != null) {
            return (Long) authentication.getPrincipal();
        }
        return null;
    }

    public static Patients getCurrentPatient() {
        Long patientId = getCurrentPatientId();
        if (patientId != null && patientsService != null) {
            return patientsService.getById(patientId);
        }
        return null;
    }
}